package com.fengye.rabbit.login.controller;

import com.fengye.rabbit.login.dto.UserLoginDTO;
import com.fengye.rabbit.login.response.BaseResponse;
import com.fengye.rabbit.login.response.StatusCode;
import com.fengye.rabbit.login.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author sky
 * @date 2021年06月09日 3:32 下午
 */
@RestController
@Slf4j
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping(value = "/login", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public BaseResponse login(@RequestBody @Validated UserLoginDTO dto, BindingResult result) {
        log.info("controller接收到 json:{}", dto);
        if (result.hasErrors()) {
            return new BaseResponse(StatusCode.InvalidParams);
        }

        BaseResponse response;

        boolean res = userService.login(dto);

        if (res) {
            response = new BaseResponse(StatusCode.Success.getCode(), "登录成功");
        } else {
            response = new BaseResponse(StatusCode.Fail.getCode(), "登录失败，账号/密码不匹配");
        }

        return response;
    }
}
